package cn.lanqiao.bigDate.web.controller;


import cn.lanqiao.bigDate.entity.sys.SysMenu;
import cn.lanqiao.bigDate.reslut.Result;
import cn.lanqiao.bigDate.reslut.TreeNode;
import cn.lanqiao.bigDate.web.BaseController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.List;

@RestController
@RequestMapping(SysMenuController.BASE_URL)
public class SysMenuController extends BaseController {
    public static final String BASE_URL = "sysMenu";
    /**
     * 页面跳转
     * @return
     */
    @RequestMapping(value = "toIndex")
    public ModelAndView toIndex() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName(BASE_URL+ "/index"); //指定路径
        return mav;
    }

    /**‘
     * 查询出所有菜单
     * @param menu
     * @return
     */
    @GetMapping(value = "findAllSysMenu")
    public Result<List<TreeNode>> findAllSysMenu(SysMenu menu,String topName) {
        Result<List<TreeNode>> result = new Result<>();
        List<TreeNode> list = sysMenuService.findAllSysMenu(menu,topName);
        if(list!=null){
            result.setData(list);
            result.setCode(200);
            result.setMsg("成功");
        }else{
            result.setCode(500);
            result.setMsg("失败");
        }
        return result;
    }




    /**
     * 按id查询菜单
     * @param id
     * @return
     */
    @GetMapping(value = "findSysMenuOne")
    public Result<SysMenu>  findSysMenuOne(Long id) {
        Result<SysMenu> result = new Result<>();

        SysMenu sm =  sysMenuService.findSysMenuOne(id);
        if(sm!=null){
            result.setData(sm);
            result.setCode(200);
            result.setMsg("成功");
        }else{
            result.setCode(500);
            result.setMsg("失败");
        }
        return result;
    }


    /**
     * 添加菜单
     * @param sysMenu
     * @return
     */
    @RequestMapping(value = "addSysMenu",method = RequestMethod.POST)
    public Result<TreeNode> addSysres(SysMenu sysMenu){
        Result<TreeNode> result = new Result<>();
        TreeNode treeNode= sysMenuService.addSysMenu(sysMenu);
        if(sysMenu!=null){
            result.setData(treeNode);
            result.setCode(200);
            result.setMsg("成功");
        }else{
            result.setCode(500);
            result.setMsg("失败");
        }
        return result;
    }

    /**
     * 添加菜单
     * @param sysMenu
     * @return
     */
    @RequestMapping(value = "updateSysMenu",method = RequestMethod.POST)
    public Result<TreeNode> updateSysMenu(SysMenu sysMenu){
        Result<TreeNode> result = new Result<>();
        TreeNode treeNode= sysMenuService.updateSysMenu(sysMenu);
        if(sysMenu!=null){
            result.setData(treeNode);
            result.setCode(200);
            result.setMsg("成功");
        }else{
            result.setCode(500);
            result.setMsg("失败");
        }
        return result;
    }


    /**
     * 删除菜单
     * @param id
     * @return
     */
    @RequestMapping(value = "deleteSysMenu",method = RequestMethod.GET)
    public Result<String> SysMenu(Long id){
        Result<String> result = new Result<>();
        boolean isDelete = sysMenuService.deleteSysMenu(id);
        if(isDelete){
            result.setCode(200);
            result.setMsg("成功!");
        }else{
            result.setCode(500);
            result.setMsg("失败");
        }
        return result;
    }


}



